const {
    mysql
  } = require('../../database/mysql')
  const Record = require('../../model/record')
  const Uuid = require('../../utils/uuid')
  
  class RecordService {
    //添加新球场
    static async newRecord({
      ctx
    }) {
      let newRecordInfo = {}
      newRecordInfo = ctx.request.body
      const record = new Record(newRecordInfo)
      record.id = new Uuid().uuid
      record.createdDate = new Date()
      const result = await mysql('record').insert(record.getData().record)
      return result[0] === 0 ? {
        result: true,
        record: record.getData().record
      } : {
        result: false
      }
    }
  
    //获取所有球场信息
    static async getAllRecord({ctx}) {
      let newRecordInfo = {}
      newRecordInfo = ctx.request.body
      const record = new Record(newRecordInfo)
      const result = await mysql('record').where(record.getData().recordWithNoNull).select().orderBy('created_date', 'desc')
      const RecordsInfo = []
      result.forEach((RecordInfo) => {
        RecordsInfo.push(RecordInfo)
      })
      return RecordsInfo
    }

    // 根据id找用户
  static async getRecordById({ id }) {
    console.log(id)
  
      const [recordInfo] = await mysql('record')
        .where({ id })
        .select()
    if(recordInfo) {
      const record = new Record(recordInfo)
      return record.getData()
    }else{
      return false
    }
  }

   //检索记录是否存在
static async getRecordHas({ Job_id,user_id }) {
  let recordInfo = {}
  await mysql('record')
    .where({
      Job_id,user_id
    })
    .select()
    .then((raw) => {
      [recordInfo] = raw
    })
    if(recordInfo){
      const record = new Record(recordInfo)
      return record.getData()
    }else{
      return undefined
    }
}
  
      //获取所有球场信息(带订单)
      static async getRecordWithOrder({
        day1,day2,day3
     }) {
        const result = await mysql('record').select('record.*',
        {'day1':await mysql('order').select().where('record_id','=','record.id').andWhere({date:day1})
        },
        {'day2':
          await mysql('order').select().where('record_id','=','record.id').andWhere({date:day2})
        },
        {'day3':
          await mysql('order').select().where('record_id','=','record.id').andWhere({date:day3})
        }
        )
        const RecordsInfo = []
        result.forEach((RecordInfo) => {
          RecordsInfo.push(RecordInfo)
        })
        return RecordsInfo
      }
    // 删除某个球场
    static async delRecord({
      id
    }) {
      const result = await mysql('record')
        .where({
          id
        })
        .del()
  
      return result === 1
    }
    // 球场信息更新
    static async setRecord({
      ctx,
      id
    }) {
      let updateRecordInfo = {}
      updateRecordInfo = ctx.request.body
      const record = new Record(updateRecordInfo)
      const updateInfo = record.getData().recordWithNoNull
      Reflect.deleteProperty(updateInfo, 'id')
      Reflect.deleteProperty(updateInfo, 'created_date')
      Reflect.deleteProperty(updateInfo, 'updated_date')
      const result = await mysql('record').where({
        id
      }).update(updateInfo)
      return result === 1
    }
  }
  
  
  module.exports = RecordService